//
//  XAPageView.swift
//  XAPageViewDemo
//
//  Created by XangAm on 2017/12/28.
//  Copyright © 2017年 Lan. All rights reserved.
//

import UIKit


 //当前的PageView的模式,不同的模式使用的控件不同
fileprivate enum PageViewModel : Int{
    case content = 0   //内容形式,需要提供子控制器
    case sectionPage   //组页形式,需要提供数据源和布局
}

@objc protocol XAPageViewDataSource{
    @objc optional  func pageViewOnSectionPageNumOfSection() -> Int
    @objc optional  func pageView(_ pageView : XAPageView , onSectionPageNumOfItemsInSection section : Int ) -> Int
    @objc optional  func pageView(_ pageView : XAPageView , onSectionPageCellForItemAt indexPath : IndexPath ) -> UICollectionViewCell
}

@objc protocol XAPageViewDelegate{
     @objc optional  func pageView(_ pageView: XAPageView, onSectionPageDidSelectItemAt indexPath: IndexPath)
}


class XAPageView: UIView {
    fileprivate weak var parentVc : UIViewController?
    fileprivate var style    : XAPageViewStyle
    fileprivate var titles   : [String]
 
    //.contentView
    fileprivate var model : PageViewModel
    fileprivate var childVcs : [UIViewController]!
    fileprivate var contentView : XAContentView!
    
    //.sectionView
    fileprivate var sectionPageView    : XASectionPageView!
    fileprivate var sectionPageLayout  : XASectionPageLayout!
    fileprivate var sectionPageDelegate   : XAPageViewDelegate?
    fileprivate var sectionPageDataSource : XAPageViewDataSource?
   
    fileprivate lazy var titleView   : XATitleView = {
        let titleFrame = CGRect(x: 0, y: 0, width: bounds.width,height:style.titleViewH)
        let titleView = XATitleView(frame:titleFrame, style: style, titles: titles)
        titleView.backgroundColor = UIColor.rondomColor
        return titleView
    }()
  
    
    init(frame : CGRect, style : XAPageViewStyle,parentVc : UIViewController,titles : [String],childVcs : [UIViewController]) {
        self.model  = .content
        self.titles = titles
        self.style  = style
        self.childVcs = childVcs
        self.parentVc = parentVc
        
        super.init(frame: frame)
        setupContentSubviews()
    }
    
    init(frame : CGRect, style : XAPageViewStyle,titles : [String], dataSource : XAPageViewDataSource?, delegate : XAPageViewDelegate?,layout : XASectionPageLayout) {
        self.model  = .sectionPage
        self.titles = titles
        self.style  = style
        self.sectionPageLayout = layout
        self.sectionPageDataSource = dataSource
        self.sectionPageDelegate   = delegate
        super.init(frame: frame)
        setupSectionPageSubviews()
    }
    
    required init?(coder aDecoder: NSCoder) {
        return nil
    }
    

}


//MARK: - Open
extension XAPageView {
    open func register(_ cellClass: Swift.AnyClass?, forCellWithReuseIdentifier identifier: String){
        sectionPageView.register(cellClass, forCellWithReuseIdentifier: identifier)
        
    }
    open func register(_ nib: UINib?, forCellWithReuseIdentifier identifier: String){
        
        sectionPageView.register(nib, forCellWithReuseIdentifier: identifier)
    }
    open func dequeueReusableCell(withReuseIdentifier identifier: String, for indexPath: IndexPath) -> UICollectionViewCell{
        
        return sectionPageView.dequeueReusableCell(withReuseIdentifier: identifier, for: indexPath)
    }
}


//MARK: - Setup
extension XAPageView {
    
    fileprivate func setupContentSubviews(){
        
        //init titleView
        addSubview(titleView)
        
        
        //init contentView
        let contentFrame = CGRect(x: 0, y: style.titleViewH, width: bounds.width, height: bounds.height - style.titleViewH)
        contentView = XAContentView(frame: contentFrame, parentVc:parentVc, style: style, childVcs: childVcs)
        contentView.backgroundColor = UIColor.rondomColor
        addSubview(contentView)
        
        titleView.delegate = contentView
        contentView.delegate = titleView
        
    }
    
    
    fileprivate func setupSectionPageSubviews(){
        
        //init titleView
      
        addSubview(titleView)
        
   
        
        //init contentView
        let sectionPageFrame = CGRect(x: 0, y: style.titleViewH, width: bounds.width, height: bounds.height - style.titleViewH)
        sectionPageView = XASectionPageView(frame: sectionPageFrame, layout: self.sectionPageLayout,dataSource:sectionPageDataSource ,delegate: sectionPageDelegate)
        sectionPageView.backgroundColor = UIColor.rondomColor
        addSubview(sectionPageView)
        
        titleView.delegate = sectionPageView
        sectionPageView.sectionPageViewDelegate = titleView
        
    }
}

